package com.luzhikun.myshopping.controller;

import com.luzhikun.myshopping.dao.UserDao;
import com.luzhikun.myshopping.dao.impl.UserDaoImpl;
import com.luzhikun.myshopping.domaim.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
@WebServlet("/loginServlet")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        String action = req.getParameter("action");
        System.out.println(action);
        if (action == null ) {
            return;
        }
        if ("login".equals(action)){
            userLogin(req,resp);
        }
        if ("logout".equals(action)){
            userLogout( req,  resp);
        }
        if ("regist".equals(action)){
            userRegist(req,resp);
        }
    }

    /*
     * @Author liu-miss
     * @Description 用户注册
     * @Date 20:45 2021/7/12
     * @Param [req, resp]
     * @return void
     **/

    private void userRegist(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if (username!=null && !"".equals(username) && password!=null && !"".equals(password)){
            UserDao userDao = new UserDaoImpl();
            boolean b = userDao.userRegist(username, password);
            String msg="";
            System.out.println(b);
            if (b ){
                try {
                    req.getRequestDispatcher("/login.jsp").forward(req,resp);
                } catch (IOException | ServletException e) {
                    e.printStackTrace();
                }
            }else {
                try {
                    msg="注册失败";
                    req.setAttribute("msg",msg);
                    req.getRequestDispatcher("/regist.jsp").forward(req,resp);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    /*
     * @Author liu-miss
     * @Description 用户登录
     * @Date 17:32 2021/7/12
     * @Param []
     * @return void
     **/

    private void userLogin(HttpServletRequest req,HttpServletResponse resp) throws UnsupportedEncodingException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println(username);
        System.out.println(password);
        if (username!=null && !"".equals(username) && password!=null && !"".equals(password)){
            UserDao userDao = new UserDaoImpl();
            List<User> users = userDao.userLogin(username, password);
            System.out.println(users);
            if (users!=null){
                try {
                    req.getRequestDispatcher("/show").forward(req,resp);
                } catch (IOException | ServletException e) {
                    e.printStackTrace();
                }
            }else {
                try {
                    req.getRequestDispatcher("/login.jsp").forward(req,resp);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /*
     * @Author liu-miss
     * @Description 退出登录
     * @Date 23:07 2021/7/12
     * @Param [req, resp]
     * @return void
     **/
    private void userLogout(HttpServletRequest req,HttpServletResponse resp) {
        try {
            resp.sendRedirect("login.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
